package com.zbkj.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.zbkj.admin.service.AdminProductHandler;
import com.zbkj.common.enums.ProductTypeEnum;
import com.zbkj.common.exception.CommonException;
import com.zbkj.common.model.product.Product;
import com.zbkj.common.model.project.ProjectInfo;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.product.AdminProductQueryRequest;
import com.zbkj.common.request.product.AdminProductSaveRequest;
import com.zbkj.common.vo.product.ProductQueryVo;
import com.zbkj.service.service.product.ProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @Auther:
 * @Date: 2024/9/12 13:52
 * @Description:
 */
@Service
public class AdminProductHandlerImpl implements AdminProductHandler {

    @Autowired
    private ProductService productService;

    @Override
    public PageInfo<Product> list(AdminProductQueryRequest queryRequest) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getDeleteFlag,0);
        if(StringUtils.isNotBlank(queryRequest.getKeyword())) {
            queryWrapper.like(Product::getProductName, queryRequest.getKeyword());
        }
        if(Objects.nonNull(queryRequest.getProductType())) {
            queryWrapper.eq(Product::getProductType, queryRequest.getProductType());
        }
        if(Objects.nonNull(queryRequest.getStatus())) {
            queryWrapper.eq(Product::getProductStatus, queryRequest.getStatus());
        }
        queryWrapper.orderByDesc(Product::getId);
        Page<Product> pageObj = PageHelper.startPage(queryRequest.getPage(), queryRequest.getLimit());
        List<Product> list = productService.list(queryWrapper);
        return CommonPage.copyPageInfo(pageObj, list);
    }

//    @Override
//    public void saveOrUpdate(AdminProductSaveRequest productRequest) {
//        if(Objects.isNull(productRequest.getId())) { //新增
//            Product saveProduct = new Product();
//            BeanUtils.copyProperties(productRequest,saveProduct);
//            saveProduct.setProductStatus(1);
//            productService.saveOrUpdate(saveProduct);
//        } else {//更新
//            Product productDb = productService.getByProductId(productRequest.getId());
//            if(Objects.isNull(productDb)) {
//                throw new CommonException("商品信息已不存在");
//            }
//            if(productDb.getProductStatus().intValue() == 0) {
//                throw new CommonException("商品上架中，不能进行修改");
//            }
//            Product upProduct = new Product();
//            BeanUtils.copyProperties(productRequest,upProduct);
//            productService.updateById(upProduct);
//        }
//    }

    @Override
    public void saveOrUpdate(AdminProductSaveRequest productRequest) {
        if(Objects.isNull(productRequest.getId())) { //新增
            Product saveProduct = new Product();
            BeanUtils.copyProperties(productRequest,saveProduct);
            saveProduct.setProductStatus(1); //新增的商品默认下架状态
            //如果是试用包，设置单价为0
            if(productRequest.getProductType() == 2) { //2代表试用包
                saveProduct.setProductPrice(new BigDecimal(0));
            }
            productService.saveOrUpdate(saveProduct);
        } else { //更新
            Product productDb = productService.getByProductId(productRequest.getId());
            if(Objects.isNull(productDb)) {
                throw new CommonException("商品信息已不存在");
            }
            if(productDb.getProductStatus().intValue() == 0) {
                throw new CommonException("商品上架中，不能进行修改");
            }
            Product upProduct = new Product();
            BeanUtils.copyProperties(productRequest,upProduct);
            //如果是试用包，更新时也确保单价为0
            if(upProduct.getProductType() == 2) { //2代表试用包
                upProduct.setProductPrice(new BigDecimal(0));
            }
            productService.updateById(upProduct);
        }
    }
    @Override
    public Product getInfo(Long id) {
        Product product = productService.getByProductId(id);
        if(Objects.isNull(product)) {
            throw new CommonException("商品信息已不存在");
        }
        return product;
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Product product = productService.getByProductId(id);
        if(Objects.isNull(product)) {
            throw new CommonException("商品信息已不存在");
        }
        if(product.getProductStatus().intValue() == status.intValue()) {
            return;
        }
        if(status.intValue() !=0 && status.intValue() != 1) {
            throw new CommonException("商品状态参数错误");
        }
        if(ProductTypeEnum.isType1(product.getProductType()) && status.intValue() == 0) {
            int rowCount = productService.getByProductTypeCount(ProductTypeEnum.PTYPE1.getType(), Lists.newArrayList(0));
            if(rowCount > 0) {
                throw new CommonException("已存在上架中的订阅包");
            }
        }
        if(ProductTypeEnum.isType2(product.getProductType()) && status.intValue() == 0) {
            int rowCount = productService.getByProductTypeCount(ProductTypeEnum.PTYPE2.getType(), Lists.newArrayList(0));
            if(rowCount > 0) {
                throw new CommonException("已存在上架中的试用包");
            }
        }
        Product upProduct = new Product();
        upProduct.setId(id);
        upProduct.setProductStatus(status);
        productService.updateById(upProduct);
    }

    @Override
    public void delProduct(Long id) {
        Product product = productService.getByProductId(id);
        if(Objects.isNull(product)) {
            throw new CommonException("商品信息已不存在");
        }
        if(product.getProductStatus().intValue() == 0) {
            throw new CommonException("商品上架中，不能进行删除");
        }
        Product upProduct = new Product();
        upProduct.setId(id);
        upProduct.setDeleteFlag(1);
        productService.updateById(upProduct);
    }

    @Override
    public List<Product> getBuyProductList(Integer queryType) {
        ProductQueryVo queryVo = new ProductQueryVo();
        queryVo.setProductStatus(0);
        if(queryType == 1) {
            queryVo.setProductTypeList(ProductTypeEnum.getFlowDataTypeList());
        } else {
            queryVo.setProductTypeList(ProductTypeEnum.getReportDataTypeList());
        }
        return productService.getProductList(queryVo);
    }
}
